//
//  CSSStatusViewModel.swift
//  weibo28
//
//  Created by chushushan on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//cell对应的viewmodel
class CSSStatusViewModel: NSObject {
    
    var status: CSSStatus?
    
    
    //转发微博内容
    var retweetContent: String?
    //转发数内容
    var retweetCountContent: String?
    //评论数
    var commentCountContent: String?
    //赞
    var unlikeCountContent: String?
    
    //来源
    var sourceContent: String?
    
    //会员等级图片
    var mbrankImage: UIImage?
    
    //认证等级图片
    var verifiedTypeImage: UIImage?
    
    //时间 只读属性，拿发微博时间和当前时间比较
    var timeContent: String? {
    
        get {
            guard let creatAt = status?.created_at else {
            return nil
            }
            
            return NSDate.sinaData(creatAt).sinaDateString
        
/*            //返回的时间格式
            //("Mon May 16 20:16:31 +0800 2016")
            //格式化时间
            let dateFormatter = NSDateFormatter()
            dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
            dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
            
            let createAtDate = dateFormatter.dateFromString(creatAt)!
//            print(createAtDate)
            
            //判断是否是今年
            if isThisYear(createAtDate) {
            //是今年
                //获取当前日历
                let calendar = NSCalendar.currentCalendar()
                //判断是否是今天
                if calendar.isDateInToday(createAtDate) {
                    //  判断是否在1分钟之内
                    //                //  获取时间差值的方式1
                    //                // createAtDate.timeIntervalSinceDate(<#T##anotherDate: NSDate##NSDate#>)
                    //                //  获取时间差值的方式2
                    //  使用abs 或者 '-' 取正数
                    let timeInterval = abs(createAtDate.timeIntervalSinceNow)
                    
                    if timeInterval < 60 {
                        
                        return "刚刚"
                    } else if timeInterval < 3600 {
                        
                        let result = timeInterval / 60
                        return "\(Int(result))分钟前"
                        
                    } else {
                    
                        let result = timeInterval / 3600
                        return "\(Int(result))小时前"
                    
                    }
                    
                    
                
                } else if calendar.isDateInYesterday(createAtDate) {
                //昨天
                    dateFormatter.dateFormat = "昨天 HH:mm"
                
                } else {
                    //其他
                    dateFormatter.dateFormat = "MM-dd HH:mm"
                
                }
            
            } else {
            //不是今年
                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
            
            }
            
            return dateFormatter.stringFromDate(createAtDate)
  */
        }

    }
    
    
    
    
    init(status: CSSStatus) {
        
        super.init()
        self.status = status
        
        handleRetWeetContent(status)
        
        //转发
        retweetCountContent = handleCountContent(status.reposts_count, defaultTitle: "转发")
        commentCountContent = handleCountContent(status.comments_count, defaultTitle: "评论")
        unlikeCountContent = handleCountContent(status.attitudes_count, defaultTitle: "赞")
        
        handleSourceCoutent(status)

        //会员等级图片处理
        handleMBrankImage(status.user?.mbrank ?? 0)

        //认证类型等级图片处理
        handleVerifiedTypeImage(status.user?.verified_type ?? 0)
    }
    
    
    //转发微博内容拼接字符串
    private func handleRetWeetContent(status: CSSStatus) {
    
        guard let retweet = status.retweeted_status?.text else {
        
        return
        }
        
        guard let name = status.retweeted_status?.user?.screen_name else {
        
        return
        }
        
        retweetContent = "@\(name): \(retweet)"
    
    }
    
    
    //处理转发，评论，赞的数据
    private func handleCountContent(count: Int, defaultTitle: String) -> String {
    
        if count > 0 {
        
            if count >= 10000 {
            
                let result = CGFloat(count / 1000 / 10)
                let resultStr = "\(result)万"
                
                if resultStr.containsString(".0") {
                
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                
                return resultStr
                
            } else {
            
            return "\(count)"
            }
        
        } else {
        
        return defaultTitle
        }
        
    
    }
    
    //处理来源数据
    private func handleSourceCoutent(status: CSSStatus) {
    
        guard let source = status.source else {
        
        return
        }
        
        if source.containsString("\">") {
        
        let startIndex = source.rangeOfString("\">")?.endIndex
            
            let endIndex = source.rangeOfString("</")?.startIndex
            
            if let start = startIndex, end = endIndex {
            
                sourceContent = source.substringWithRange(start..<end)
            }
        }
    
    }
    
    
    
    
//    
//    //根据发微博时间判断是否是今年
//    private func isThisYear(createAtDate: NSDate) -> Bool {
//    
//        //获取当前时间
//        let currentDate = NSDate()
//        let dateFormatter = NSDateFormatter()
//        dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
//        //格式化时间
//        dateFormatter.dateFormat = "yyyy"
//        
//        //获取当前年份
//        let currentYear = dateFormatter.stringFromDate(currentDate)
//        //获取发微博年份
//        let createAtYear = dateFormatter.stringFromDate(createAtDate)
//        
//        return currentYear == createAtYear
//    
//    }
    
    
    //处理会员等级图片
    private func handleMBrankImage(count: Int) {
    
        if count > 0 && count < 7 {
        mbrankImage = UIImage(named: "common_icon_membership_level\(count)")
        
        }
    
    }
    
    //处理认证类型等级图片
    private func handleVerifiedTypeImage(verifiedType: Int) {
        //-1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
        let verified = verifiedType
        
        switch verified {
        
        case 0 :
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2, 3, 5 :
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220 :
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default :
            verifiedTypeImage = nil
        }
    
    }
    
    
    
}












